package com.cwj;

import java.util.*;

/**
 * @author chenwujie
 * @date 2020/09/2911:04
 */
public class SetTest {
    public static void main(String[] args) {
        SetTest setTest = new SetTest();
        int i = setTest.maxLength(Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"));
        System.out.println(i);
    }

    public int maxLength(List<String> arr) {
        if(arr == null || arr.size() == 0){
            return 0;
        }

        int[] maskArr = new int[arr.size()];
        for(int i = 0; i < arr.size(); i++){
            maskArr[i] = str2int(arr.get(i));
        }
        return dfs(arr, maskArr, 0, 0);

    }

    public int dfs(List<String> arr, int[] maskArr, int start, int mask){
        if(start == arr.size()){
            return 0;
        }
        int ans = 0;
        for(int i = start; i < arr.size(); i++){
            if(maskArr[i] == 0 || (maskArr[i] & mask) > 0){
                continue;
            }
            ans = Math.max(ans, dfs(arr, maskArr, i + 1, mask | maskArr[i]) + arr.get(i).length());
        }
        return ans;
    }

    public int str2int(String s){
        char[] arr = s.toCharArray();
        int n = 0;
        for(char c : arr){
            int mask = 1 << (c-'a');
            if((n & mask) > 0){
                return 0;
            }
            n |=  mask;
        }
        return n;
    }
}
